package oa.hleast.xswl.service.activity.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.xswl.mapper.activity.CmsActivityTemplateMapper;
import oa.hleast.xswl.pojo.domain.activity.CmsActivityTemplate;
import oa.hleast.xswl.service.activity.ICmsActivityTemplateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@Transactional
@AllArgsConstructor
public class CmsActivityTemplateServiceImpl extends ServiceImpl<CmsActivityTemplateMapper, CmsActivityTemplate> implements ICmsActivityTemplateService {


    @Override
    public boolean add(CmsActivityTemplate form) {
        boolean save = this.save(form);

        return save;
    }

    @Override
    public boolean update(CmsActivityTemplate form) {
        return this.updateById(form);
    }

    @Override
    public Page<CmsActivityTemplate> pageList(Integer page, Integer limit, Long activityId, Integer display) {

        LambdaQueryWrapper<CmsActivityTemplate> queryWrapper = new LambdaQueryWrapper<CmsActivityTemplate>()
                .eq(CmsActivityTemplate::getScenesId, activityId)
                .orderByDesc(CmsActivityTemplate::getGmtCreate)
                .orderByDesc(CmsActivityTemplate::getGmtModified);
        Page<CmsActivityTemplate> templatePage = this.page(new Page<>(page, limit), queryWrapper);
        log.info("templatePage = {}", templatePage);
        return templatePage;
    }

    @Override
    public boolean deleteByIds(List<String> asList) {
        return this.removeByIds(asList);
    }

    @Override
    public CmsActivityTemplate detail(Long id) {
        return this.getById(id);
    }

    @Override
    public CmsActivityTemplate detailByActivityId(Long activityId) {
        LambdaQueryWrapper<CmsActivityTemplate> queryWrapper = new LambdaQueryWrapper<CmsActivityTemplate>()
                .eq(CmsActivityTemplate::getScenesId, activityId)
                .last("limit 1");
        List<CmsActivityTemplate> list = this.list(queryWrapper);
        if (list.size() != 1) {
            throw new BizException("报名表异常，请联系管理员！");
        }
        return list.get(0);
    }

    @Override
    public CmsActivityTemplate getTemplateByActivityId(Long activityId) {
        LambdaQueryWrapper<CmsActivityTemplate> queryWrapper = new LambdaQueryWrapper<CmsActivityTemplate>()
                .eq(CmsActivityTemplate::getScenesId, activityId)
                .last("limit 1");
        List<CmsActivityTemplate> list = this.list(queryWrapper);
        if (list.size() == 0) {
            return null;
        }

        return list.get(0);
    }


}
